/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.pinyin.handler;

import com.google.common.base.Preconditions;
import com.hyts.guava.String.Strings;
import com.hyts.pinyin.base.PinyinHandler;
import com.hyts.pinyin.factory.ChineseCharacterFactory;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.pinyin.bean
 * @author:LiBo/Alex
 * @create-date:2019-08-05 16:30
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class ChineseCharToPinyin implements PinyinHandler<HanyuPinyinOutputFormat,String> {

    /**
     * 转换操作服务
     * @param paramWord
     * @return
     */
    @Override
    public String transfer(String paramWord) {
        Preconditions.checkArgument(Strings.isNotEmpty(paramWord),"传入的转换字符串的汉字不可以为空！");
        try {
            return PinyinHelper.toHanYuPinyinString(paramWord,ChineseCharacterFactory.createLowerCase(),
                    " ",true);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return "";
    }

    /**
     * 转换操作服务
     * @param paramWord
     * @param splitWord
     * @return
     */
    @Override
    public String transfer(String paramWord,String splitWord) {
        Preconditions.checkArgument(Strings.isNotEmpty(paramWord),"传入的转换字符串的汉字不可以为空！");
        try {
            return PinyinHelper.toHanYuPinyinString(paramWord,ChineseCharacterFactory.createLowerCase(),
                    Strings.compareEmptyAndSet(splitWord," "),true);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return "";
    }

    /**
     * 转换操作服务
     * @param param
     * @param paramWord
     * @param splitWord
     * @return
     */
    @Override
    public String transfer(HanyuPinyinOutputFormat param, String paramWord,String splitWord) {
        Preconditions.checkArgument(Strings.isNotEmpty(paramWord),"传入的转换字符串的汉字不可以为空！");
        try {
            return PinyinHelper.toHanYuPinyinString(paramWord,param,
                    Strings.compareEmptyAndSet(splitWord," "),true);
        } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
            badHanyuPinyinOutputFormatCombination.printStackTrace();
        }
        return "";
    }
}
